package com.phone580.app.process;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Debug;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.phone580.app.BaseActivity;
import com.phone580.app.R;
import com.phone580.app.entity.RunningAppInfoEntity;
import com.phone580.app.untilty.CommonUtility;

public class ProcessManageActivity extends BaseActivity {
	private List<RunningAppInfoEntity> runningAppInfos;
//	private PackageManager pm;
	private ActivityManager am;
	private ListView lvProgress;
	private ProgressBar pbLoader;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			pbLoader.setVisibility(View.GONE);
			ProcessListAdapter adapter = new ProcessListAdapter(ProcessManageActivity.this, runningAppInfos);
			adapter.setActivityManager(am);
			lvProgress.setAdapter(adapter);
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_process_layout);
		initHeader("进程管理");
		lvProgress = (ListView) findViewById(R.id.user_process_lv);
		pbLoader = (ProgressBar) findViewById(R.id.user_process_pb);
//		pm = getPackageManager();
		am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		new LoadApp().start();
		
	}
	
	class LoadApp extends Thread {
		@Override
		public void run() {
			
			// 查询所有已经安装的应用程序
//			List<PackageInfo> apps = new ArrayList<PackageInfo>();
//			PackageManager pManager = getPackageManager();
//			// 获取手机内所有应用
//			List<PackageInfo> paklist = pManager.getInstalledPackages(0);
//			for (int i = 0; i < paklist.size(); i++) {
//				PackageInfo pak = (PackageInfo) paklist.get(i);
//				// 判断是否为非系统预装的应用程序
//				if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
//					// customs applications
//					apps.add(pak);
//				}
//			}
//
//			// 保存所有正在运行的包名 以及它所在的进程信息
//			Map<String, ActivityManager.RunningAppProcessInfo> pgkProcessAppMap = new HashMap<String, ActivityManager.RunningAppProcessInfo>();
//
//			ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
//			// 通过调用ActivityManager的getRunningAppProcesses()方法获得系统里所有正在运行的进程
//			List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager
//					.getRunningAppProcesses();
//
//			for (ActivityManager.RunningAppProcessInfo appProcess : appProcessList) {
////				int pid = appProcess.pid; // pid
////				String processName = appProcess.processName; // 进程名
//
//				String[] pkgNameList = appProcess.pkgList; // 获得运行在该进程里的所有应用程序包
//
//				// 输出所有应用程序的包名
//				for (int i = 0; i < pkgNameList.length; i++) {
//					String pkgName = pkgNameList[i];
//					// 加入至map对象里
//					pgkProcessAppMap.put(pkgName, appProcess);
//				}
//			}
//			// 保存所有正在运行的应用程序信息
//			runningAppInfos = new ArrayList<RunningAppInfoEntity>(); // 保存过滤查到的AppInfo
//
//			for (PackageInfo pkgInfo : apps) {
//				// 如果该包名存在 则构造一个RunningAppInfo对象
//				if (pgkProcessAppMap.containsKey(pkgInfo.packageName)) {
//					// 获得该packageName的 pid 和 processName
//					int pid = pgkProcessAppMap.get(pkgInfo.packageName).pid;
//					String processName = pgkProcessAppMap.get(pkgInfo.packageName).processName;
//					runningAppInfos.add(getAppInfo(pkgInfo.applicationInfo, pid, processName));
//				}
//			}
			runningAppInfos = CommonUtility.getRunningApps(ProcessManageActivity.this);
			handler.sendEmptyMessage(0);
		}
		
//		private RunningAppInfoEntity getAppInfo(ApplicationInfo app, int pid, String processName) {
//			RunningAppInfoEntity appInfo = new RunningAppInfoEntity();
//			appInfo.setAppLabel((String) app.loadLabel(pm));
//			appInfo.setAppIcon(app.loadIcon(pm));
//			appInfo.setPkgName(app.packageName);
//
//			appInfo.setPid(pid);
//			appInfo.setProcessName(processName);
//			
//			int[] myMempid = new int[] { pid };
//			// 此MemoryInfo位于android.os.Debug.MemoryInfo包中，用来统计进程的内存信息
//			Debug.MemoryInfo[] memoryInfo =  am.getProcessMemoryInfo(myMempid);
//			// 获取进程占内存用信息 kb单位
//			int memSize = memoryInfo[0].dalvikPrivateDirty;
//			appInfo.setMemSize(memSize);
//			return appInfo;
//		}
	}

	public void refresh() {
		pbLoader.setVisibility(View.VISIBLE);
		new LoadApp().start();
	}

}
